package com.uenpay.im.invokeitems.chat;

import android.text.TextUtils;

import com.uenpay.im.ui.old.activity.group.GroupType;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class AcceptJoinGroupInvokeItem extends BaseHttpInvokeItem {
	public class AcceptJoinGroupInvokeItemResult extends HttpInvokeResult {
		public int status;
		public String msg;
	}

	public AcceptJoinGroupInvokeItemResult getOutput() {
		return (AcceptJoinGroupInvokeItemResult) GetResultObject();
	}

	/**
	 * 接收加入群聊
	 * 
	 * @param type
	 * @param notice_id
	 * @param accept
	 * @param paypassword
	 *            支付密码
	 */
	public AcceptJoinGroupInvokeItem(int type, String notice_id, int accept,
			String paypassword, String chatid,String invitecustid) {
		String url = "";
		if (type == GroupType.NORMAL_GROUP) {
			/**普通群**/
			url = APIConfiguration.getBaseUrl()
					+ "Chat/Chat/acceptJoin?noticeid=" + notice_id + "&accept="
					+ accept + "&" + APIConfiguration.getCustIdAndToken();
		} else {
			/**邀请进入**/
			if (!TextUtils.isEmpty(notice_id)) {
				url = APIConfiguration.getBaseUrl()
						+ "Chat/SpecialChat/acceptInvite?contributid="
						+ notice_id + "&accept=" + accept + "&paypassword="
						+ paypassword + "&invitecustid="+invitecustid+"&"
						+ APIConfiguration.getCustIdAndToken();
			} else {
				/**扫描二维码进入群聊**/
				url = APIConfiguration.getBaseUrl()
						+ "Chat/SpecialChat/joinChat?chatid=" + chatid
						+ "&paypassword=" + paypassword + "&"
						+ APIConfiguration.getCustIdAndToken();
			}

		}
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		AcceptJoinGroupInvokeItemResult result = new AcceptJoinGroupInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

}
